package com.ruoyi.web.controller.fdhController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.web.entity.dto.EmployeeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author FDH
 * @Date 2021/5/28 17:45
 **/
@Api(tags = "fdh专属")
@RestController
@CrossOrigin
@RequestMapping("/user")
@Slf4j
public class MyController {
    @Autowired
    private BizCompanyMapper bizCompanyMapper;
    @Autowired
    private RelUserCompanyMapper relUserCompanyMapper;
    @Autowired
    private BizAppConfigMapper bizAppConfigMapper;
    @Autowired
    private BizStoreMapper bizStoreMapper;
    @Autowired
    private BizEmployeeMapper bizEmployeeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;


/**
 * 企业管理
 */

    /**
     * 通过userId获取企业id
     */
    @ApiOperation("通过userId获取企业id")
    @GetMapping("/getCompanyId")
    public AjaxResult getCompanyId(Long userId){
        List<Long> companyId = relUserCompanyMapper.getCompanyId(userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("companyId",companyId);
        return ajax;
    }

    /**
     * 资质申请
     */
    @ApiOperation("申请资质")
    @PostMapping("/addBizCompany")
    public AjaxResult apply(@RequestBody BizCompany bizCompany) {
        int i = bizCompanyMapper.insertBizCompany(bizCompany);
        if (i > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @ApiOperation("企业管理列表")
    @GetMapping("/getBizCompanyList")
    public AjaxResult getCompany(BizCompany bizCompany) {
        List<BizCompany> bizCompanies = new ArrayList<>();
        bizCompanies = bizCompanyMapper.selectBizCompanyList(bizCompany);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("bizCompanies", bizCompanies);
        return ajaxResult;
    }

    @ApiOperation("查看企业信息")
    @GetMapping("/getBizCompany")
    public AjaxResult getCompanieById(Long companyId) {
        BizCompany bizCompany = bizCompanyMapper.selectBizCompanyById(companyId);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("bizCompany", bizCompany);
        return ajaxResult;
    }
    @Transactional
    @ApiOperation("审批管理")
    @PostMapping("/approvedBizCompany")
    public AjaxResult approved(@RequestBody BizCompany bizCompany) {
        int i = bizCompanyMapper.updateBizCompany(bizCompany);
        if (i > 0) {
            if ("1".equals(bizCompany.getApprovalStatus())) {
                int i1 = relUserCompanyMapper.insertBizCompany(bizCompany.getUserId(), bizCompany.getCompanyId());
                if (i1 <= 0) {
                    return AjaxResult.error("插入关联表错误");
                }
            }
            return AjaxResult.success("审批成功");

        } else {
            return AjaxResult.error("审批失败");
        }
    }

    /**
     * 应用配置
     */
    @ApiOperation("添加应用")
    @PostMapping("/addConfig")
    public AjaxResult addConfig(@RequestBody BizAppConfig bizAppConfig) {
        int i = bizAppConfigMapper.insertBizAppConfig(bizAppConfig);
        if (i > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @ApiOperation("获取商户的应用")
    @GetMapping("/getConfigList")
    public AjaxResult getConfigList(BizAppConfig bizAppConfig) {
        List<BizAppConfig> bizAppConfigs = new ArrayList<>();
        bizAppConfigs = bizAppConfigMapper.selectBizAppConfigList(bizAppConfig);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("bizAppConfigs", bizAppConfigs);
        return ajax;
    }

    @ApiOperation("查看单个应用")
    @GetMapping("/getConfig")
    public AjaxResult getConfig(Long appId) {
        BizAppConfig bizAppConfig = bizAppConfigMapper.selectBizAppConfigById(appId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("bizAppConfig", bizAppConfig);
        return ajax;
    }

    @ApiOperation("修改应用")
    @PostMapping("/updateConfig")
    public AjaxResult updateConfig(@RequestBody BizAppConfig bizAppConfig) {
        int i = bizAppConfigMapper.updateBizAppConfig(bizAppConfig);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 门店
     */
    /**
     * 添加门店
     *
     * @param bizStore
     * @return
     */

    @ApiOperation("添加门店")
    @PostMapping("/addStore")
    public AjaxResult addStore(@RequestBody BizStore bizStore) {
        System.out.println("bizStore:"+bizStore);

        System.out.println("bizStore:"+bizStore.getCompanyId());
        int i = bizStoreMapper.insertBizStore(bizStore);
        if (i > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @ApiOperation("获取门店列表")
    @GetMapping("/getStoreList")
    public AjaxResult getStoreList(BizStore bizStore) {
        List<BizStore> bizStores = new ArrayList<>();
        bizStores = bizStoreMapper.selectBizStoreList(bizStore);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("bizStores", bizStores);
        return ajax;
    }

    @ApiOperation("查看单个门店信息")
    @GetMapping("/getStore")
    public AjaxResult getStore(Long storeId) {
        BizStore bizStore = bizStoreMapper.selectBizStoreById(storeId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("bizStore", bizStore);
        return ajax;
    }

    @ApiOperation("修改门店")
    @PostMapping("/updateStore")
    public AjaxResult updateStore(@RequestBody BizStore bizStore) {
        int i = bizStoreMapper.updateBizStore(bizStore);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 员工管理
     */
    @ApiOperation("添加员工")
    @PostMapping("/addEmployee")
    public AjaxResult addEmployee(@RequestBody EmployeeDto employeeDto) {

        SysUser user = new SysUser();
        user.setUserName(employeeDto.getUserName());
        user.setPassword(SecurityUtils.encryptPassword(employeeDto.getPassword()));
        user.setNickName(employeeDto.getNickName());
        user.setPhonenumber(employeeDto.getPhonenumber());

        int i1 = sysUserMapper.insertUser(user);
        if (i1 > 0) {

            SysRole role = sysRoleMapper.getSysRoleByRoleName("普通角色");
            if (role != null) {
                SysUserRole userRole = new SysUserRole();
                userRole.setRoleId(role.getRoleId());
                userRole.setUserId(user.getUserId());
            } else {
                return AjaxResult.error("绑定普通角色失败");
            }
            BizEmployee bizEmployee = new BizEmployee();
            bizEmployee.setStoreId(employeeDto.getStoreId());
            bizEmployee.setEmployeeStoreRoleTag(employeeDto.getEmployeeStoreRoleTag());
            bizEmployee.setUserId(user.getUserId());

            int i = bizEmployeeMapper.insertBizEmployee(bizEmployee);
            if (i > 0) {
                return AjaxResult.success("添加成功");
            } else {
                return AjaxResult.error("添加失败");
            }

        } else {
            return AjaxResult.error("创建用户失败");
        }
    }

    /**
     * 通过商户老板id获取员工信息
     *
     * @param userId
     * @return
     */
    @ApiOperation("通过商户老板id获取员工信息")
    @GetMapping("/getEmployeeList")
    public AjaxResult getEmployeeList(Long userId) {
       List<EmployeeEntity> employeeEntity = bizEmployeeMapper.selectStoreEmployee(userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("employeeEntity", employeeEntity);
        return ajax;
    }



    /**
     * 修改员工信息
     */
    @ApiOperation("修改员工信息")
    @PostMapping("/updateEmployeeById")
    public AjaxResult updateEmployeeById(EmployeeDto employeeDto) {
        if (employeeDto.getNickName() != null) {
            SysUser user = new SysUser();
            user.setNickName(employeeDto.getNickName());
            user.setUserId(employeeDto.getUserId());
            int i = sysUserMapper.updateUser(user);
            if (i <= 0) {
                return AjaxResult.error("修改失败");
            }
        }
        BizEmployee bizEmployee = new BizEmployee();
        if (employeeDto.getStoreId() != null) {
            bizEmployee.setStoreId(employeeDto.getStoreId());
        }
        if (employeeDto.getEmployeeStoreRoleTag() != null) {
            bizEmployee.setEmployeeStoreRoleTag(employeeDto.getEmployeeStoreRoleTag());
        }
        if(employeeDto.getStoreId() != null || employeeDto.getEmployeeStoreRoleTag() != null){
            bizEmployee.setEmployeeId(employeeDto.getEmployeeId());
            int i = bizEmployeeMapper.updateBizEmployee(bizEmployee);
            if (i > 0){
                return AjaxResult.success("修改成功");
            }else {
                return AjaxResult.error("修改失败");
            }
        }
        return AjaxResult.success("修改成功");
    }




}
